package com.example.experiment_four;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SendMessage extends Activity {
    SmsManager sManager;
    EditText number, content;
    Button send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_message);

        number = (EditText) findViewById(R.id.phone);
        content = (EditText) findViewById(R.id.context);
        send = (Button)findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    try {
                        ActivityCompat.requestPermissions(SendMessage.this,new String[]{Manifest.permission.SEND_SMS},-1);
                        PendingIntent pi = PendingIntent.getActivity(SendMessage.this, 0, new Intent(), 0);
                        sManager.sendTextMessage(number.getText().toString(), null, content.getText().toString(), pi, null);
                        Toast.makeText(SendMessage.this, "短信发送完成", Toast.LENGTH_LONG).show();
                    } catch (Exception e) {
                        Toast.makeText(SendMessage.this, "短信发送失败", Toast.LENGTH_LONG).show();
                    }
                }
        });


    }
}
